import Vue from 'vue'
import Cookies from 'js-cookie'
import 'normalize.css/normalize.css' // A modern alternative to CSS resets
import Element from 'element-ui'
import './styles/element-variables.scss'

import '@/styles/index.scss' // global css
import 'video.js/dist/video-js.css' // 引入video的css样式
import App from './App'
import VModal from 'vue-js-modal'
import VueAMap from 'vue-amap'
import router from './router'
import store from './store'
import i18n from './lang' // Internationalization
import '@/utils/vant'
import '@/icons/svg'
import '@/icons/icon'
import '@/utils/clamp'
import VueClipboard from 'vue-clipboard2'
Vue.use(VueClipboard)
import { mapMutations } from 'vuex'
import websocket from './websocket'
Vue.use(websocket)
// 发送消息
Vue.prototype.sendSocketData = (message) => {
  window.websocket.send(message)
}
// 瀑布流
import { VueMasonryPlugin } from 'vue-masonry'
Vue.use(VueMasonryPlugin)

Vue.use(Element, {
  size: Cookies.get('size') || 'medium', // set element-ui default size
  i18n: (key, value) => i18n.t(key, value)
}).use(VModal, {
  dialog: true,
  dynamic: true,
  injectModalsContainer: true
}).use(VueAMap)
import Mixins from '@/utils/mixins'
Vue.mixin(Mixins)
// 初始化时设置登录状态  0：未登录  1：已登录
if (!localStorage.getItem('isLogin')) {
  localStorage.setItem('isLogin', 0)
}
import './permission' // permission control

import * as filters from './filters' // global filters
// register global utility filters.
Object.keys(filters).forEach((key) => {
  Vue.filter(key, filters[key])
})

import Bus from '@/utils/bus.js'
Bus(Vue)

import Tinymce from './components/Tinymce'
Vue.component('Tinymce', Tinymce)
import ImageUpload from './components/ImageUpload'
Vue.component('ImageUpload', ImageUpload)
Vue.config.productionTip = false

import JSEncrypt from 'jsencrypt'
Vue.prototype.$getRsaCode = (str) => {
  // 注册方法
  const pubKey =
    'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvCkyEkJSy7QWRPZzceERKiMEtdPfCvJbEJj93IqE4nX4CAwuNESEx5jVTBG7TfvOCbUTdcRh9MHPQ0XldVPs/HO8v95OMUXh1+bDrDBPPh3tSgh+2YZB/WxJAY6V6kNPXXnsZBHCpQqTt0INbPrjTGcVNKFphDSmuom3/TxHd5c0dH33xGSL8U0/KXTWto3QmhdAOqCErhMdn6MMNjyaU5qnHq9x0B5shEHSyp3hkBRCcnScnCRRVqTNbeJ2iiyfX6Dd8RqVA9emlj9jHetU+ot7DwwhdMDS2vT6xb32HHEYemN85bPXpjkawuHznKks0dmc5ceHxAalNpmneIVvMwIDAQAB' // ES6 模板字符串 引用 rsa 公钥
  const encryptStr = new JSEncrypt()
  encryptStr.setPublicKey(pubKey) // 设置 加密公钥
  const data = encryptStr.encrypt(str.toString()) // 进行加密
  return data
}
Vue.prototype.$filterPath = function(target) {
  const path = this.$route.fullPath.split('/')
  const length = path.length
  const replaceNum = this.$route.meta.replaceNum || 0
  return path.splice(0, length - replaceNum).join('/') + '/' + target
}
// 转换为预览url
Vue.prototype.$conversionPreviewLink = function(fileUrl) {
  // 对字符串进行base64编码
  let conversionFileUrl = window.btoa(unescape(encodeURIComponent(fileUrl)))
  // 有特殊字符的解开注释替换
  // conversionFileUrl = conversionFileUrl.replace(/\=/g, '%3D')
  // conversionFileUrl = conversionFileUrl.replace(/\&/g, '%26')
  // conversionFileUrl = conversionFileUrl.replace(/\#/g, '%23')
  // conversionFileUrl = conversionFileUrl.replace(/\%/g, '%25')
  // conversionFileUrl = conversionFileUrl.replace(/\?/g, '%3F')
  // conversionFileUrl = conversionFileUrl.replace(/\//g, '%2F')
  // conversionFileUrl = conversionFileUrl.replace(/\ /g, '%20')
  // conversionFileUrl = conversionFileUrl.replace(/\+/g, '%2B')
  // conversionFileUrl = 'https://jnqgshengji-view.bjttsx.com/preview/onlinePreview?url=' + conversionFileUrl
  conversionFileUrl = 'https://view.chineseworkers.com.cn/preview/onlinePreview?url=' + conversionFileUrl
  return conversionFileUrl
}
Date.prototype.Format = function(fmt) {
  var o = {
    'M+': this.getMonth() + 1, // 月份
    'd+': this.getDate(), // 日
    'h+': this.getHours(), // 小时
    'm+': this.getMinutes(), // 分
    's+': this.getSeconds(), // 秒
    'q+': Math.floor((this.getMonth() + 3) / 3), // 季度
    S: this.getMilliseconds() // 毫秒
  }
  if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length))
  for (var k in o) {
    if (new RegExp('(' + k + ')').test(fmt)) fmt = fmt.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length))
  }
  return fmt
}
window.$Vue = new Vue({
  el: '#app',
  router,
  store,
  i18n,
  methods: {
    ...mapMutations(['CHANG_SOCKET_DATA'])
  },
  render: (h) => h(App)
})
